import 'package:flutter/material.dart';
import '../widgets/profile_form.dart';
import '../models/user_model.dart';

class ProfileScreen extends StatefulWidget{
  const ProfileScreen({super.key});

  @override
  State<ProfileScreen> createState()=>_ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen>{
  final _formKey = GlobalKey<FormState>();
  final User _user = User.empty();

  Future<void> _submitForm() async{
    if(_formKey.currentState!.validate()){
      _formKey.currentState!.save();

      // 模拟异步操作
      await Future.delayed(const Duration(seconds: 5));

      if(context.mounted){
        showDialog(context: context,
            builder: (context)=>AlertDialog(
              title: const Text('Success'),
              content: Text('Profile saved for ${_user.name}'),
              actions: [
                TextButton(onPressed: ()=>Navigator.pop(context),
                    child: const Text('OK'),)
              ],
            ));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Edit Profile')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Stack(
              alignment: Alignment.bottomRight,
              children: [
                const CircleAvatar(radius: 60,
                backgroundImage: NetworkImage(  'https://randomuser.me/api/portraits/men/41.jpg',),),
                IconButton(onPressed: (){
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: const Text('choose an avatar'),
                    backgroundColor: Colors.blueGrey,
                    action: SnackBarAction(
                      label: 'Dismiss',
                      textColor: Colors.green,
                      onPressed: (){
                        print('dismiss Dialog');
                      },
                    ),
                  ));
                }, icon: const Icon(Icons.camera_alt,color: Colors.white))
              ],
            ),
            const SizedBox(height: 20),
            ProfileForm(formKey:_formKey,user:_user),
            const SizedBox(height: 20),
            ElevatedButton(onPressed: _submitForm,
                style: ElevatedButton.styleFrom(
                  padding: const EdgeInsets.symmetric(horizontal: 50,vertical: 15),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30),
                  )
                ),
                child: const Text('Save Profile')),
          ],
        ),
      ),
    );
  }
}
